package com.time;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Stack;

public class DateDemo {
	public static void main(String[] args) throws ParseException {
		String d1 = "2013-02-19";
		String d2 = "2012-08-19";
		//
		// getMonth(d1, d2);
		// stackDemo();
		// CalendarDemo();
		//

//		SimpleDateFormatDemo();
		System.out.println(isLeapYear(2100));
	}
	 public static boolean isLeapYear(int year) {
	       GregorianCalendar calendar = new GregorianCalendar();
	       return calendar.isLeapYear(year);
	    }

	private static void SimpleDateFormatDemo() throws ParseException {
		// 注意构造函数中是SimpleDateFormat类解析日期的模式，大小写代表的意义完全不一样哦
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss-E");
		SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		// 日期到字符串的转换
		String today = df.format(new Date());
		System.out.println(today);
		// 字符串到日期的转换
		Date date = df2.parse("2016-09-12 02:06:37");
		System.out.println(df.format(date));
	}

	private static void CalendarDemo() {
		Calendar cal = Calendar.getInstance();
		System.out.println(cal.get(Calendar.YEAR));
		System.out.print(cal.get(Calendar.MONTH) + 1);
		System.out.print(cal.get(Calendar.DAY_OF_MONTH));
		System.out.print(cal.get(Calendar.HOUR_OF_DAY));
		System.out.print(cal.get(Calendar.MINUTE));
		System.out.print(cal.get(Calendar.SECOND));
	}

	private static void stackDemo() {
		Stack<String> ss = new Stack<String>();
		ss.add("123");
		for (String string : ss) {
			System.out.println(ss.get(0));

		}
	}

	private static int getMonth(String endTime, String beginTime)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.setTime(sdf.parse(endTime));
		int year1 = c.get(Calendar.YEAR);// 2013
		int month1 = c.get(Calendar.MONTH);// 1

		c.setTime(sdf.parse(beginTime));
		int year2 = c.get(Calendar.YEAR);// 2012
		int month2 = c.get(Calendar.MONTH);// 8

		int result;
		if (year1 == year2) {
			result = month1 - month2;
		} else {
			result = 12 * (year1 - year2) + month1 - month2;
		}
		System.out.println(result);
		return result;
	}

}
